home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13730 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  62 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: reversing a string
  5. Date: Wed, 10 Apr 96 00:46:15 GMT
  6. Organization: none
  7. Message-ID: <829097175snz@genesis.demon.co.uk>
  8. References: <4k6cjl$j8f@central.server.swt.edu> <4kb1s7$6eu@ibm32.perftech.com>,<829058514snz@genesis.demon.co.uk> <4ke7a6$49m@central.server.swt.edu>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4ke7a6$49m@central.server.swt.edu>
  15.            ln16674@nyssa.swt.edu "Leland Newsom" writes:
  16.  
  17. >Yes, I was doing that.  He said that it could be done without any extra
  18. >variables.
  19.  
  20. OK, here's one way:
  21.  
  22.  
  23. #include <stdio.h>
  24.  
  25. static void reverse(char *str);
  26.  
  27. int main(int argc, char *argv[])
  28.  
  29. {
  30.     if (argc >= 2)
  31.         reverse(argv[1]);
  32.  
  33.     printf("'%s'\n", argv[1]);
  34.  
  35.     return 0;
  36. }
  37.  
  38. static void reverse(char *str)
  39.  
  40. {
  41.     if (str[0] != '\0' && str[1] != '\0') {
  42.         reverse(str+1);
  43.         reverse(str+2);
  44.         str[0] ^= str[1], str[1] ^= str[0], str[0] ^= str[1];
  45.         reverse(str+1);
  46.     }
  47. }
  48.  
  49. > He also said that if you get real cryptic you can do the function
  50. >in one or two lines.
  51.  
  52. If you wanted to you could code the body of reverse() above as a single
  53. expression. Most newlines in a C source file are simply white-space so
  54. you could put the whole function on one line if you wanted to, but it
  55. doesn't prove very much.
  56.  
  57. -- 
  58. -----------------------------------------
  59. Lawrence Kirby | fred@genesis.demon.co.uk
  60. Wilts, England | 70734.126@compuserve.com
  61. -----------------------------------------
  62.